<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 生成指定随机字符串
 * @Author   zhua
 * @DateTime 2018-08-25T10:14:40+0800
 * @param    int      	$length 取随机字符串
 * @return string
 */
function getRandChar($length = 32) {
    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;

    for ($i = 0; $i < $length; $i++) {
        $str .= $strPol[rand(0, $max)];
    }

    return $str;
}

function createDir($dir) {
    return is_dir($dir) or (createDir(dirname($dir)) and mkdir($dir, 0777));
}

function addLog($content='', $path = '')
{
	return \app\service\RecordLog::saveLog($content, $path = '');
}

function timeFormat(int $time = 0) {
    $format = [];
    if(!$time) {
        $format['date'] = date('Y-m-d', time());
        $format['date_hour'] = date('Y-m-d H', time());
        $format['date_minute'] = date('Y-m-d H:i', time());
        $format['date_time'] = date('Y-m-d H:i:s', time());
    }else{
        $format['date'] = date('Y-m-d', $time);
        $format['date_hour'] = date('Y-m-d H', $time);
        $format['date_minute'] = date('Y-m-d H:i', $time);
        $format['date_time'] = date('Y-m-d H:i:s', $time);
    }

    return $format;
}

/** 
 * 创建像这样的查询: "IN('a','b')";
 * @access   public
 * @param    mix      $item_list      列表数组或字符串
 * @param    string   $field_name     字段名称
 * @return   void
 */
function dbCreateIn($item_list, $field_name = '') {
    if (empty($item_list)) {
        return $field_name . " IN ('') ";
    } else {
        if (!is_array($item_list)) {
            $item_list = explode(',', $item_list);
        }
        $item_list = array_unique($item_list);
        $item_list_tmp = '';
        foreach ($item_list AS $item) {
            if ($item !== '') {
                $item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";
            }
        }
        return empty($item_list_tmp) ? $field_name . " IN ('') " : $field_name . ' IN (' . $item_list_tmp . ') ';
    }
}

function _print($first_param, $second_param = null, $third_param = null)
{
    echo "<pre>";
    print_r($first_param);
    $second_param && print_r($second_param);
    $third_param && print_r($third_param);
    die;
}

/**
 * 二维数组根据某个字段排序
 * @param array $array 要排序的数组
 * @param string $keys   要排序的键字段
 * @param string $sort  排序类型  SORT_ASC SORT_DESC
 * @return array 排序后的数组
 */
function arraySort($array, $keys, $sort = SORT_DESC) {
    $keysValue = [];
    foreach ($array as $k => $v) {
        $keysValue[$k] = $v[$keys];
    }
    array_multisort($keysValue, $sort, $array);
    return $array;
}